//-----------------------------------------------------------------------
// <copyright file="AddNetworkAdapter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr@microsoft.com</email>
// <date>2004-05-24</date>
// <summary>Adds all the external adapters to the virtual machine specified</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.VirtualServer.VirtualMachine
{
    #region Using directives
    using System;
    using System.Reflection;
    using System.Collections;
    using System.Globalization;
    using System.Runtime.InteropServices;
    using Microsoft.Sdc.Tasks.VirtualServer;
    //using Microsoft.Build.Framework;
    #endregion

    /// <summary>
    /// TODO
    /// Starts a virtual machine.
    /// </summary>          
    /// <remarks>
    /// <code><![CDATA[
    /// <VirtualServer.VirtualMachine.Start
    ///             MachineName="machineName"
    ///             ServerName="serverName"
    ///             Timeout="timeout">
    /// </VirtualServer.VirtualMachine.Start>
    /// ]]></code>
    /// <para>where:</para>
    /// <para><i>machineName (Required)</i></para>
    /// <para>The name of the virtual machine to be started.</para>
    /// <para><i>serverName</i></para>
    /// <para>Set the <i>serverName</i> property to specify a remote server to connect to. Defaults to connect locally.</para>
    /// <para><i>timeout</i></para>
    /// <para>Length of time to wait for the start task to complete, in milliseconds. Defaults to 10.</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <VirtualServer.VirtualMachine.Start
    ///             MachineName="myMachine"
    ///             Timeout="20"
    ///         </VirtualServer.VirtualMachine.Start> 
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    public class AddNetworkAdapter : VirtualServerBase
    {
        private string serverName;
        private string machineName;
       
        /// <summary>
        /// Initializes a new instance of the VirtualServer.AddNetworkAdapter class.
        /// </summary>
        public AddNetworkAdapter()
        {

        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            if (this.machineName == null || this.machineName.Length == 0)
            {
                throw new TaskException("VirtualServer.InvalidMachineName", null);
            }
            
            ComWrapper virtualServer = GetVirtualServer(this.serverName);
            ComWrapper virtualNetworks = new ComWrapper(virtualServer.GetProperty("VirtualNetworks"), true);

            int count = (int)virtualNetworks.GetProperty("Count");

            if (count == 0)
            {
                throw new TaskException("VirtualServer.TODONoVirtualNetworks", null);
            }
            
            ComWrapper virtualMachines = GetVirtualMachinesFromServer(this.serverName);
            ComWrapper virtualMachine = GetVirtualMachine(virtualMachines, this.machineName);

            NativeMethods.VMState state = (NativeMethods.VMState)virtualMachine.GetProperty("State");

            if (state != NativeMethods.VMState.VMState_TurnedOff)
            {
                throw new TaskException("VirtualServer.MachineRunning", MachineName);
            }

            ComWrapper networkAdapter = null;
            ComWrapper virtualNetwork = null;

            for (int i = 0; i < count; i++)
            {
                virtualNetwork = new ComWrapper(virtualNetworks.GetProperty("Item", i + 1), true);

                string indexNetworkName = ((string)virtualNetwork.GetProperty("Name")).ToLowerInvariant();

                if (indexNetworkName.StartsWith("external"))
                {
                    networkAdapter = new ComWrapper(virtualMachine.CallMethod("AddNetworkAdapter"), true);
                    //Attach to all external adapters
                    networkAdapter.CallMethod("AttachToVirtualNetwork", new object[] {virtualNetwork.ComObject});
                }
            }            
        }

        /// <summary>
        /// Set the <i>serverName</i> property to specify a remote server to connect to.
        /// </summary>
        /// <value>Set the <i>serverName</i> property to specify a remote server to connect to. Defaults to connect locally.</value>
        public string ServerName
        {
            get
            {
                return (serverName == null ? String.Empty : serverName);
            }
            set
            {
                serverName = value;
            }
        }

        /// <summary>
        /// The name of the virtual machine to be started.
        /// </summary>
        /// <value>The name of the virtual machine to be started.</value>
        //[Required]
        public string MachineName
        {
            get
            {
                return (machineName == null ? String.Empty : machineName);
            }
            set
            {
                machineName = value;
            }
        }
    }
}
